package yxy.game.pm2.module.guard;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.bean.module.RedisLock;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

public class GuardPlayerModule extends PlayerModule {

	public static enum GuardOperate {
		OP_激励(0), OP_医疗(1), OP_急救(2), OP_袭营(3), OP_攻城(4),;

		private GuardOperate(int value) {
		}

		public static GuardOperate forNumber(int number) {
			switch (number) {
			case 0:
				return OP_激励;
			case 1:
				return OP_医疗;
			case 2:
				return OP_急救;
			case 3:
				return OP_袭营;
			case 4:
				return OP_攻城;
			default:
				return OP_激励;
			}
		}

	}

	// bean===========================================================================

	private DateTime startTime;
	private HashMap<GuardOperate, DateTime> coolDown = new HashMap<>();// 操作冷却时间
	private boolean rewarded;// 是否领取过奖励

	public DateTime getStartTime() {
		return startTime;
	}

	public void setStartTime(DateTime startTime) {
		this.startTime = startTime;
	}

	public HashMap<GuardOperate, DateTime> getCoolDown() {
		return coolDown;
	}

	public void setCoolDown(HashMap<GuardOperate, DateTime> coolDown) {
		this.coolDown = coolDown;
	}

	public boolean isRewarded() {
		return rewarded;
	}

	public void setRewarded(boolean rewarded) {
		this.rewarded = rewarded;
	}

	// func===========================================================================

	public Collection<Long> operationList() {
		ArrayList<Long> list = new ArrayList<>();
		list.add(coolDown.get(GuardOperate.OP_激励).getMillis());
		list.add(coolDown.get(GuardOperate.OP_医疗).getMillis());
		list.add(coolDown.get(GuardOperate.OP_急救).getMillis());
		list.add(coolDown.get(GuardOperate.OP_袭营).getMillis());
		list.add(coolDown.get(GuardOperate.OP_攻城).getMillis());
		return list;
	}

	public void update() {
		GuardModule guardModule = GuardModule.get(getPlayerCode());
		if (startTime == null || startTime.getMillis() != guardModule.getStartTime().getMillis()) {
			setStartTime(guardModule.getStartTime());
			coolDown.clear();
			coolDown.put(GuardOperate.OP_激励, startTime);
			coolDown.put(GuardOperate.OP_医疗, startTime);
			coolDown.put(GuardOperate.OP_急救, startTime);
			coolDown.put(GuardOperate.OP_袭营, startTime);
			coolDown.put(GuardOperate.OP_攻城, startTime);
			setRewarded(false);
			set();
		}
	}

	// redis==========================================================================

	private static String key(String playerCode) {
		return String.format("PLAYER:%s:MODULE:GUARD", playerCode);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public GuardPlayerModule get(String playerCode) {
		try {
			GuardPlayerModule module = getv(key(playerCode));
			if (module == null) {
				module = MySQLCacheDAO.getV(key(playerCode), GuardPlayerModule.class);
				if(module != null){module.set();}//把数据同步到redis
			}

			if (module == null) {
				module = create(playerCode);
			}
			module.update();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	static private GuardPlayerModule create(String playerCode) {
		GuardPlayerModule module = new GuardPlayerModule();
		module.setPlayerCode(playerCode);
		module.set();
		return module;
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				GuardPlayerModule guardPlayerModule = getv(key(playerCode));
				if(guardPlayerModule != null) MySQLCacheDAO.setV(key, guardPlayerModule);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return null;
	}

	public static RedisLock lock(String playerCode) {
		return getLock(String.format("lock:%s", key(playerCode)));
	}

}
